<?PHP
require_once WACT_ROOT.'controller/controller.inc.php';

require_once APP_ROOT.'view/pub/layoutright.view.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/article.php';
require_once APP_ROOT.'model/common/price.php';
require_once APP_ROOT.'model/common/logobanners.php';
require_once APP_ROOT.'model/common/date.php';
require_once APP_ROOT . "model/common/site.php";



class ArticlesDetailView extends LayoutRightView {
	function ArticlesDetailView($TemplateFile) {
		parent::LayoutRightView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare(&$controller, &$request, &$responseModel);

		if(!Session::isLogin())
			$this->Template->set('login',' ');

		$article_id=$request->getParameter('id');
		$_SESSION['article_id'] = $article_id;
		$this->Template->set('article_id',$article_id);

		$article=&Article::getRecord($article_id);
		if(Session::isLogin()&&Session::getUserId()==$article->get('author_user_id'))
			$this->Template->set('article_author',' ');


		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Article - '.$article->get('headline'));


      


		$datestr=$article->get('date_added');
		$mydate=strtotime($datestr);
        $tz =& new MyTimeZone();
        $mydate = $tz->timeGMT($mydate);
		$datestr = gmstrftime('%b %d, %Y',$mydate);
		$this->Template->set('date_added',$datestr);

		$prem=$article->get('premium');
		if($prem&&$prem==1)
			$this->Template->set('prem',$prem);

		$this->Template->set('headline',$article->get('headline'));
		$this->Template->set('firstname',$article->get('firstname'));
		$this->Template->set('lastname',$article->get('lastname'));
		$this->Template->set('author_title',$article->get('title'));
		$description=$article->get('description');

		$this->Template->set('description',$description);

		if($article->get('form_id') && $article->get('form_id')>0) {
			$this->Template->set('feedback',' ');
			$this->Template->set('form_id',$article->get('form_id'));
		}

		if($article->get('add_banner')&&$article->get('add_banner')>0){
			if(!$article->get('big_picture')||$article->get('big_picture')==''){
				$banner=LogoBannersModel::getBanner('ART');
				$this->Template->set($banner[0],$banner[1]);
			}
		}

		// app_name is set in the parent class
		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

	}

}



class ArticlesTeaserView extends LayoutRightView {
	function ArticlesTeaserView($TemplateFile){
		parent::LayoutRightView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare(&$controller, &$request, &$responseModel);

		if(!Session::isLogin())
			$this->Template->set('login',' ');

		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Become a Subscriber');


        

		$article_id=$request->getParameter('id');
		$this->Template->set('article_id',$article_id);
		$article=&Article::getRecord($article_id);

		$datestr=$article->get('date_added');
		$mydate=strtotime($datestr);
        $tz =& new MyTimeZone();
        $mydate = $tz->timeGMT($mydate);
		$datestr = gmstrftime('%b %d, %Y',$mydate);
		$this->Template->set('date_added',$datestr);

		$prem=$article->get('premium');
		if($prem&&$prem==0) $prem=null;
		$this->Template->set('prem',$prem);

		$this->Template->set('headline',$article->get('headline'));
		$this->Template->set('firstname',$article->get('firstname'));
		$this->Template->set('lastname',$article->get('lastname'));
		$this->Template->set('author_title',$article->get('title'));
		$teaser=$article->get('teaser');
		if($teaser&&strlen($teaser)>0){
			//$tes=$this->Template->getChild('teaser');

			$this->Template->set('teaser',$teaser);
		}
		$price =& Price::getPrice(4);
		$price1 =& Price::getPrice(1);
		$price2 =& Price::getPrice(2);
		$price3 =& Price::getPrice(3);

		$price = number_format($price,2);
		$price1 = number_format($price1,2);
		$price2 = number_format($price2,2);
		$price3 = number_format($price3,2);
		$this->Template->set('price',$price);
		$this->Template->set('price1',$price1);
		$this->Template->set('price2',$price2);
		$this->Template->set('price3',$price3);
                // app_name is set in the parent class
		if(APP_NAME!="") {
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');
	}

}


class ArticlesDetailPage extends PageController {
	function ArticlesDetailPage(){
		parent::PageController();

		$showdetail=false;


		$article_id=Request::getParameter('id');
		$articles=&Article::getRecord($article_id);

		if((!$articles->get('premium'))||($articles->get('premium')==0))
			$showdetail=true;

		else if(Session::isLogin()){
			$user_id=Session::getUserId();
			if(User::isUserSubscriptionActive($user_id)){
				$showdetail=true;
			}
			else if(User::isUserAdmin($user_id)){
				$showdetail=true;
			}

			if($articles->get('author_user_id')==$user_id)
					$showdetail=true;

		}

        $site_id = Site::getTemplateSiteID();
        $mytemplate1 = 'pub/articles/detail.html';
        $mytemplate2 = 'pub/articles/teaser.html';

		if($showdetail)
			$this->setDefaultView(new ArticlesDetailView($mytemplate1));
		else
			$this->setDefaultView(new ArticlesTeaserView($mytemplate2));
	}


}

?>
